﻿using System;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.Text;

namespace SurveyListsAdministration.ControlTemplates.SurveyListsAdministration
{
    public partial class SurveyListStatus : UserControl
    {
        public SPWeb Web
        {
            get
            {
                return SPContext.Current.Web;
            }
        }

        public SPList List
        {
            get
            {
                return SPContext.Current.List;
            }
        }

        public bool CurrentContextIsSurvey
        {
            get
            {
                return this.List != null && this.List.BaseTemplate == SPListTemplateType.Survey;
            }
        }

        public bool IsValidFormMode
        {
            get
            {
                bool isValidFormMode = false;
                switch (SPContext.Current.FormContext.FormMode)
                {
                    case SPControlMode.Display:
                    case SPControlMode.Edit:
                    case SPControlMode.New:
                        isValidFormMode = false;
                        break;
                    case SPControlMode.Invalid:
                        isValidFormMode = true;
                        break;
                }

                return isValidFormMode;
            }
        }


        public bool SurveyHasUncompletedResponses
        {
            get
            {
                return NumberOfItemNotPublished != 0;
            }
        }

        private int? _numberOfItemNotPublished;
        public int NumberOfItemNotPublished
        {
            get
            {
                if (!_numberOfItemNotPublished.HasValue)
                {
                    int itemCount = this.List.ItemCount;
                    int itemPublished = GetNumberOfItemPublished();
                    _numberOfItemNotPublished = itemCount - itemPublished;
                }

                return _numberOfItemNotPublished.Value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            base.CreateChildControls();
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            ScriptLink.RegisterScriptAfterUI(this.Page, "core.js", true, false);
            ScriptLink.RegisterScriptAfterUI(this.Page, "CUI.js", false, true);
            ScriptLink.RegisterScriptAfterUI(this.Page, "SP.js", false, true);

            if (this.CurrentContextIsSurvey && this.IsValidFormMode && this.SurveyHasUncompletedResponses)
            {
                ShowStatus();
            }
        }

        private void ShowStatus()
        {
            string script = BuildScript();
            this.Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "showstatus", script.ToString(), true);
        }

        private int GetNumberOfItemPublished()
        {
            int numberOfItemsPublished = 0;
            using (SPSite site = SPTools.GetHPSite(this.Web.Url))
            {
                using(SPWeb web = site.OpenWeb())
                {
                    SPList list = web.Lists[this.List.ID];
                    numberOfItemsPublished = list.Items.Count;
                }
            }

            return numberOfItemsPublished;
        }

        private string BuildScript()
        {
            string statusMessage = SPTools.GetLocalizedString(this.Web, "SurveyStatus_Message"); ;
            string statusTitle = GetStatusTitle();
            string functionName = "addSurveyListtatus";

            StringBuilder script = new StringBuilder();

            script.AppendFormat("SP.SOD.executeOrDelayUntilScriptLoaded({0}, 'SP.js');", functionName);
            script.AppendFormat("function {0}()", functionName);
            script.Append("{");
            script.AppendFormat("var statusId = SP.UI.Status.addStatus('{0}', '{1}', true); ", statusTitle, statusMessage);
            script.Append("setStatusPriColor(statusId, 'blue');");
            script.Append("}");

            return script.ToString();
        }

        private string GetStatusTitle()
        {
            string statusTitleFormat = SPTools.GetLocalizedString(this.Web, "SurveyStatus_Title");
            string statusTitle = string.Format(statusTitleFormat, this.NumberOfItemNotPublished);
            return statusTitle;
        }
    }
}

